package com.xoa.dev.weLink.service;

import com.xoa.dao.common.SysParaMapper;
import com.xoa.dao.users.UsersMapper;
import com.xoa.dao.weLink.UserWeLinkMapper;
import com.xoa.model.common.SysPara;
import com.xoa.model.users.Users;
import com.xoa.model.weLink.UserWeLink;
import com.xoa.model.weLink.UserWeLinkExample;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.wrapper.BaseWrapper;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;
import sun.swing.StringUIClientPropertyKey;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WeLinkConfigService {

    @Resource
    private SysParaMapper sysParaMapper;

    @Resource
    private UserWeLinkMapper userWeLinkMapper;

    @Resource
    private UsersMapper usersMapper;

    @Resource
    private WeLinkService weLinkService;

    public BaseWrapper getWeLinkConfig(){
        BaseWrapper baseWrapper = new BaseWrapper();
        Map<String,String> resultMap = new HashMap<>();
        SysPara welinkAppid = sysParaMapper.querySysPara("WELINK_APPID");
        if(welinkAppid!=null){
            resultMap.put("weLinkAppId",welinkAppid.getParaValue());
        }
        SysPara welinkAppsecret = sysParaMapper.querySysPara("WELINK_APPSECRET");
        if(welinkAppsecret!=null){
            resultMap.put("weLinkAppSecret",welinkAppsecret.getParaValue());
        }
        baseWrapper.setFlag(true);
        baseWrapper.setData(resultMap);
        return baseWrapper;
    }

    public BaseWrapper setWeLinkConfig(String weLinkAppId,String weLinkAppSecret){
        BaseWrapper baseWrapper = new BaseWrapper();

        SysPara welinkAppid = sysParaMapper.querySysPara("WELINK_APPID");
        if(welinkAppid!=null){
            welinkAppid.setParaValue(weLinkAppId);
            sysParaMapper.updateSysPara(welinkAppid);
        }else{
            welinkAppid = new SysPara();
            welinkAppid.setParaName("WELINK_APPID");
            welinkAppid.setParaValue(weLinkAppId);
            sysParaMapper.insertSysPara(welinkAppid);
        }


        SysPara welinkAppsecret = sysParaMapper.querySysPara("WELINK_APPSECRET");
        if(welinkAppsecret!=null){
            welinkAppsecret.setParaValue(weLinkAppSecret);
            sysParaMapper.updateSysPara(welinkAppsecret);
        }else{
            welinkAppsecret = new SysPara();
            welinkAppsecret.setParaName("WELINK_APPSECRET");
            welinkAppsecret.setParaValue(weLinkAppSecret);
            sysParaMapper.insertSysPara(welinkAppsecret);
        }

        baseWrapper.setFlag(true);
        return baseWrapper;
    }

    /**
     * @接口说明: 绑定用户
     * @日期: 2020/3/13
     * @作者: 张航宁
     */
    public BaseWrapper bindOaUser(UserWeLink userWeLink){
        BaseWrapper baseWrapper = new BaseWrapper();
        UserWeLink userWeLink1 = userWeLinkMapper.selectByWlUserId(userWeLink.getWlUserId());
        if(userWeLink1!=null){
            userWeLinkMapper.updateByWlUserId(userWeLink);
        } else {
            UserWeLink userWeLink2 = userWeLinkMapper.selectByOaUserId(userWeLink.getOaUserId());
            if(userWeLink2!=null){
                userWeLinkMapper.updateByOaUserId(userWeLink);
            } else {
                userWeLinkMapper.insertSelective(userWeLink);
            }
        }
        baseWrapper.setFlag(true);
        return baseWrapper;
    }

    /**
     * @接口说明: 解绑用户
     * @日期: 2020/3/13
     * @作者: 张航宁
     */
    public BaseWrapper removeBindOaUser(UserWeLink userWeLink){
        BaseWrapper baseWrapper = new BaseWrapper();
        UserWeLinkExample example = new UserWeLinkExample();
        UserWeLinkExample.Criteria criteria = example.createCriteria();
        criteria.andOaUserIdEqualTo(userWeLink.getOaUserId());
        UserWeLinkExample.Criteria criteria1 = example.createCriteria();
        criteria1.andWlUserIdEqualTo(userWeLink.getWlUserId());
        example.or(criteria1);
        int i = userWeLinkMapper.deleteByExample(example);
        if(i>0){
            baseWrapper.setFlag(true);
        }
        return baseWrapper;
    }

    /**
     * @接口说明: 获取带有绑定状态的用户
     * @日期: 2020/3/16
     * @作者: 张航宁
     */
    public BaseWrapper selUsersWithBindType(String deptId,String page,String pageSize){
        BaseWrapper baseWrapper = new BaseWrapper();

        JSONArray users = weLinkService.getUsersByDeptId(deptId, page, pageSize);
        if(users.size()>0){
            List<UserWeLink> resList = new ArrayList<>();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < users.size(); i++) {
                UserWeLink userWeLink = new UserWeLink();
                JSONObject jsonObject = users.getJSONObject(i);
                String userId = jsonObject.getString("userId");
                userWeLink.setWlUserId(userId);
                userWeLink.setUserName(jsonObject.getString("userNameCn"));
                sb.append(userId).append(",");
                resList.add(userWeLink);
            }
            List<UserWeLink> userWeLinks = userWeLinkMapper.selectByWlUserIds(sb.toString().split(","));

            Map<String,String> bindMap = new HashMap<>();
            for (int i = 0; i < userWeLinks.size(); i++) {
                UserWeLink userWeLink = userWeLinks.get(i);
                if(userWeLink!=null&&!StringUtils.checkNull(userWeLink.getWlUserId())&&!StringUtils.checkNull(userWeLink.getOaUserId())){
                    bindMap.put(userWeLink.getWlUserId(),userWeLink.getOaUserId());
                }
            }

            for (int i = 0; i < resList.size(); i++) {
                UserWeLink userWeLink = resList.get(i);
                String bindOaUserId = bindMap.get(userWeLink.getWlUserId());
                if(!StringUtils.checkNull(bindOaUserId)){
                    userWeLink.setOaUserId(bindOaUserId);
                    userWeLink.setIfBind("1");
                    Users users1 = usersMapper.SimplefindUsersByuserId(bindOaUserId);
                    if(users1!=null){
                        userWeLink.setBindUserName(users1.getUserName());
                    } else {
                        userWeLink.setBindUserName("未知用户");
                    }
                } else {
                    userWeLink.setIfBind("0");
                }
            }

            baseWrapper.setFlag(true);
            baseWrapper.setData(resList);

        }

        return baseWrapper;
    }

}
